package com.hmyd.hmyd_android.view;

import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.weigets.AbstractSpinnerAdapter.IOnItemSelectListener;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.PopupWindow;
import android.widget.RelativeLayout.LayoutParams;

/**
 * 自定义弹出下拉菜单
 * 
 * @author wangk
 */
public class SelectedPopWindow extends PopupWindow implements
		OnItemClickListener{

	private Context mContext;
	private ListView listview;
	private IOnItemSelectListener iOnItemSelectListener;
	private ArrayAdapter<String> adapter;

	public SelectedPopWindow(Context mContext) {
		super(mContext);
		this.mContext = mContext;
		init();
	}

	public void setItemListener(IOnItemSelectListener listener) {
		iOnItemSelectListener = listener;
	}

	private void init() {
		View view = LayoutInflater.from(mContext).inflate(
				R.layout.view_mpopwindow_listview, null);
		setContentView(view);
		setWidth(LayoutParams.WRAP_CONTENT);
		setHeight(LayoutParams.WRAP_CONTENT);

		setFocusable(true);
		
		setBackgroundDrawable(new ColorDrawable(Color.alpha(R.color.tran)));

		listview = (ListView) view.findViewById(R.id.mListView);
		listview.setOnItemClickListener(this);
	}

	public void setAdapter(ArrayAdapter<String> adapter) {
		this.adapter = adapter;
		listview.setAdapter(adapter);
	}
	
	@Override
	public void showAsDropDown(View anchor) {
		super.showAsDropDown(anchor);
		
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		dismiss();
		if (iOnItemSelectListener != null) {
			iOnItemSelectListener.onItemClick(position);
		}
	}

}
